package com.ag.admin.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ag.admin.dao.AdminTbUserDAO;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;

/**
 * 
 * 
 * @author 温镇澎
 * 
 */
@Service("queryUserServiceImpl")
public class QueryUserServiceImpl {
	private static final Logger logger = Logger
			.getLogger(QueryUserServiceImpl.class);

	@Resource(name = "adminTbUserDAO")
	private AdminTbUserDAO userDAO;

	public PageUtil query(Map<String, String> queryOption) throws Exception {
		StringBuilder countsql = new StringBuilder();
		StringBuilder hsql = new StringBuilder();
		//countsql.append("select count(distinct u) from TbUser u join u.tbUserState us join u.tbUserRoles ur join ur.tbRole r " +
		//		"join u.tbUserDepts ud join ud.tbUser where u.a = ");
		countsql.append("select count(distinct u) from TbUser u left join u.tbUserRoles ur left join ur.tbRole r " +
				 " left join u.tbUserDepts ud left join ud.tbDeptType where u.a= ");
		hsql.append("select distinct u from TbUser u left join fetch u.tbUserState left join fetch u.tbUserRoles ur left join fetch ur.tbRole r " +
				" left join fetch u.tbUserDepts ud left join fetch ud.tbDeptType where u.a= ");
		countsql.append(IDUtil.PRESERVE);
		hsql.append(IDUtil.PRESERVE);
		List result = null;
		int firstRow = 0;
		int maxRow = 10;
		int page = 1;
		int total = 0;
		if (!(queryOption == null)) {
			// 获取总数
			total = ((Long) this.userDAO.queryCount(
					spliceHQL(countsql, queryOption).toString()).iterator()
					.next()).intValue();
			if (queryOption.containsKey("page")
					&& !queryOption.get("page").isEmpty()) {
				page = Integer.parseInt(queryOption.get("page"));
				logger.info("page=" + page);
				firstRow = maxRow * (page - 1);
			}
			if (total != 0) {
				result = this.userDAO.query(spliceHQL(hsql, queryOption)
						.toString()+" order by u.oaCode ", firstRow, maxRow);
				logger.info("result=" + result.size());
			}

		} else {
			// 第一次查询,无参数查询
			logger.info("queryOption=" + queryOption + "---");
			total = ((Long) this.userDAO.queryCount(countsql.toString())
					.iterator().next()).intValue();
			if (total != 0) {
				//result = this.userDAO.query(hsql.toString()+" order by u.UId ", firstRow, maxRow);
				result = this.userDAO.query(hsql.toString()+" order by u.oaCode", firstRow, maxRow);
				logger.info("result=" + result.size());
			}
		}
		logger.info("total=" + total);
		PageUtil pageUitl = new PageUtil();
		pageUitl.setMaxRow(maxRow);
		pageUitl.setPage(page);
		pageUitl.setTotal(total);
		pageUitl.setList(result);
		return pageUitl;
	}

	private StringBuilder spliceHQL(StringBuilder sql,
			Map<String, String> queryOption) {
		if (queryOption.containsKey("UId") && !queryOption.get("UId").isEmpty()) {
			sql.append(" and u.UId = '");
			sql.append(queryOption.get("UId") + "'");
		}
		if (queryOption.containsKey("UName")
				&& !queryOption.get("UName").isEmpty()) {
			sql.append(" and u.UName like '%");
			sql.append(queryOption.get("UName") + "%'");
		}
		if (queryOption.containsKey("URole")
				&& !"0".equals(queryOption.get("URole"))) {
			sql.append(" and r.RId = '");
			sql.append(queryOption.get("URole") + "'");
		}
		if (queryOption.containsKey("UState")
				&& !"0".equals(queryOption.get("UState"))) {
			sql.append(" and us.usId = '");
			sql.append(queryOption.get("UState") + "'");
		}
		if (queryOption.containsKey("UDept")
				&& !"0".equals(queryOption.get("UDept"))) {
			sql.append(" and ud.udId in ( '");
			sql.append(queryOption.get("UDept") + "')");
		}
		logger.info("sql = " + sql);
		return sql;
	}
}
